#include <reg52.h>
//ADC0809 模数转换显示
#define uchar unsigned char
#define uint unsigned int
uchar code LEDData[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
sbit OE = P1 ^ 0;
sbit EOC = P1 ^ 1;
sbit ST = P1 ^ 2;
sbit CLK = P1 ^ 3;
void DelayMS(uint ms)
{
    uchar i;
    while (ms--)
        for (i = 0; i < 112; i++)
            ;
}

void Display_Result(uchar m)
{
    uint d = m;
    /**********根据当前输入量m大小 ，在【0-255】所占比例，转换为当前数字量 **********/
    float k = d * 1.00;
    k = (k / 256 * 5 * 1000); //1000代表当前所设定电阻的值。256代表整体【0-255】的长度
    d = k;
    /*****P0口分别显示个、十、百、千等位的大小（段码），P2显示的位置（位码）***********/
    P3 = 0x7f; //个 01111111
    P0 = LEDData[d % 10];
    DelayMS(10);
    P3 = 0xbf; //十 10111111
    P0 = LEDData[d / 10 % 10];
    DelayMS(10);
    P3 = 0xdf; //百 11011111
    P0 = LEDData[d / 100 % 10];
    DelayMS(10);
    P3 = 0xef; //千 11101111
    P0 = LEDData[d / 1000] | 0x80;
    DelayMS(10);
}

void main()
{
    TMOD = 0x02;
    TH0 = 0x14;
    TL0 = 0x00;
    // TH0 = 246; //定时器初值高8位
    // TL0 = 246; //定时器初值低8位
    EA = 1;
    ET0 = 1; //开定时器中断
    TR0 = 1;

    while (1)
    {
        ST = 0;
        ST = 1;
        ST = 0;
        while (EOC == 0)
            ;
        OE = 1;
        Display_Result(P2);
        OE = 0;
    }
}
void Timer0_INT() interrupt 1
{
    CLK = ~CLK;
}
